﻿using System;
using System.Data;
using System.Data.SqlClient;
using Microsoft.Practices.EnterpriseLibrary.Data.Sql;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Csla;
using System.Collections;
using System.Collections.Generic;
using System.Reflection;

namespace RMSM
{
	[Serializable()]
	public class KBPhauThuatBO : Csla.BusinessBase<KBPhauThuatBO>
	{
		#region Business Properties
		
private int? _KBPhauThuatID = null;
public int? KBPhauThuatID
{
get { return this._KBPhauThuatID; }
set { this._KBPhauThuatID = value; PropertyHasChanged("KBPhauThuatID"); }
}

private string _MaSo = null;
public string MaSo
{
get { return this._MaSo; }
set { this._MaSo = value; PropertyHasChanged("MaSo"); }
}

private int? _KhoaID = null;
public int? KhoaID
{
get { return this._KhoaID; }
set { this._KhoaID = value; PropertyHasChanged("KhoaID"); }
}

private int? _BenhNhanID = null;
public int? BenhNhanID
{
get { return this._BenhNhanID; }
set { this._BenhNhanID = value; PropertyHasChanged("BenhNhanID"); }
}

private string _HoTen = null;
public string HoTen
{
get { return this._HoTen; }
set { this._HoTen = value; PropertyHasChanged("HoTen"); }
}

private string _DiaChi = null;
public string DiaChi
{
get { return this._DiaChi; }
set { this._DiaChi = value; PropertyHasChanged("DiaChi"); }
}

private string _GioiTinh = null;
public string GioiTinh
{
get { return this._GioiTinh; }
set { this._GioiTinh = value; PropertyHasChanged("GioiTinh"); }
}

private int? _DoiTuongID = null;
public int? DoiTuongID
{
get { return this._DoiTuongID; }
set { this._DoiTuongID = value; PropertyHasChanged("DoiTuongID"); }
}

private int? _NguonXuatID = null;
public int? NguonXuatID
{
get { return this._NguonXuatID; }
set { this._NguonXuatID = value; PropertyHasChanged("NguonXuatID"); }
}

private DateTime? _NgayLapPhieu = null;
public DateTime? NgayLapPhieu
{
get { return this._NgayLapPhieu; }
set { this._NgayLapPhieu = value; PropertyHasChanged("NgayLapPhieu"); }
}

private DateTime? _NgayThucHien = null;
public DateTime? NgayThucHien
{
get { return this._NgayThucHien; }
set { this._NgayThucHien = value; PropertyHasChanged("NgayThucHien"); }
}

private string _NoiDung = null;
public string NoiDung
{
get { return this._NoiDung; }
set { this._NoiDung = value; PropertyHasChanged("NoiDung"); }
}

private string _GhiChu = null;
public string GhiChu
{
get { return this._GhiChu; }
set { this._GhiChu = value; PropertyHasChanged("GhiChu"); }
}

private int? _NguoiLapPhieuID = null;
public int? NguoiLapPhieuID
{
get { return this._NguoiLapPhieuID; }
set { this._NguoiLapPhieuID = value; PropertyHasChanged("NguoiLapPhieuID"); }
}

private int? _NguoiPhuTrachID = null;
public int? NguoiPhuTrachID
{
get { return this._NguoiPhuTrachID; }
set { this._NguoiPhuTrachID = value; PropertyHasChanged("NguoiPhuTrachID"); }
}

private double? _ThanhTien = null;
public double? ThanhTien
{
get { return this._ThanhTien; }
set { this._ThanhTien = value; PropertyHasChanged("ThanhTien"); }
}

private string _BangChu = null;
public string BangChu
{
get { return this._BangChu; }
set { this._BangChu = value; PropertyHasChanged("BangChu"); }
}

private double? _TienPhatSinh = null;
public double? TienPhatSinh
{
get { return this._TienPhatSinh; }
set { this._TienPhatSinh = value; PropertyHasChanged("TienPhatSinh"); }
}

private string _LyDoPhatSinh = null;
public string LyDoPhatSinh
{
get { return this._LyDoPhatSinh; }
set { this._LyDoPhatSinh = value; PropertyHasChanged("LyDoPhatSinh"); }
}

private int? _ThanhToan = null;
public int? ThanhToan
{
get { return this._ThanhToan; }
set { this._ThanhToan = value; PropertyHasChanged("ThanhToan"); }
}

private int? _ThucHien = null;
public int? ThucHien
{
get { return this._ThucHien; }
set { this._ThucHien = value; PropertyHasChanged("ThucHien"); }
}

private int? _UserCreated = Session.LoginID;
public int? UserCreated
{
get { return this._UserCreated; }
set { this._UserCreated = value; PropertyHasChanged("UserCreated"); }
}

private DateTime? _DateCreated = DateTime.MaxValue;
public DateTime? DateCreated
{
get { return this._DateCreated; }
set { this._DateCreated = value; PropertyHasChanged("DateCreated"); }
}

private int? _UserModify = null;
public int? UserModify
{
get { return this._UserModify; }
set { this._UserModify = value; PropertyHasChanged("UserModify"); }
}

private DateTime? _DateModify = null;
public DateTime? DateModify
{
get { return this._DateModify; }
set { this._DateModify = value; PropertyHasChanged("DateModify"); }
}

private bool? _Deleted = false;
public bool? Deleted
{
get { return this._Deleted; }
set { this._Deleted = value; PropertyHasChanged("Deleted"); }
}

		private int? _STT0 = 0;
		public int? STT0
		{
			get { return _STT0; }
			set { _STT0 = value; }
		}
		
		protected override object GetIdValue()
		{
			return _KBPhauThuatID.Value;
		}
		#endregion Business Properties
		//==========================================================================
		
		#region Factory Methods
		public void SetIsNew()
		{
			MarkNew();
		}

		public void SetIsOld()
		{
			MarkOld();
		}
		
		public void SetIsDirty()
        {
            MarkDirty();
        }

        public void SetIsChild()
        {
            MarkAsChild();
        }

        public object GetValue(string fieldName)
        {
            PropertyInfo propInfo = this.GetType().GetProperty(fieldName);
            return propInfo.GetValue(this, null);
        }

        public void SetValue(string fieldName, object value)
        {
            PropertyInfo propInfo = this.GetType().GetProperty(fieldName);
            propInfo.SetValue(this, value, null);
            MarkDirty();
        }
		
		private static int newKBPhauThuatID =  -1;
private KBPhauThuatBO()
{
_KBPhauThuatID = newKBPhauThuatID--;
}

		public KBPhauThuatBO(SqlDataReader dataReader)
		{
			MarkAsChild();
			Fetch(dataReader);
			MarkOld();
		}

		public KBPhauThuatBO(SqlDataReader dataReader, bool isRoot)
		{
			if (!isRoot)
                MarkAsChild();
			Fetch(dataReader);
			MarkOld();
		}

		public static KBPhauThuatBO NewRootKBPhauThuatBO()
		{
			return DataPortal.Create<KBPhauThuatBO>(new RootCriteria());
		}

		internal static KBPhauThuatBO NewChildKBPhauThuatBO()
		{
			return DataPortal.Create<KBPhauThuatBO>(new ChildCriteria());
		}

		public static KBPhauThuatBO GetRootKBPhauThuatBO(int _KBPhauThuatID)
		{
			return DataPortal.Fetch<KBPhauThuatBO>(new RootCriteria(_KBPhauThuatID));
		}

		public static KBPhauThuatBO GetRootKBPhauThuatBOByWhere(string where)
		{
			return DataPortal.Fetch<KBPhauThuatBO>(where);
		}

		internal static KBPhauThuatBO GetChildKBPhauThuatBO(SqlDataReader dataReader)
		{
			return new KBPhauThuatBO(dataReader);
		}

		public static void DeleteKBPhauThuatBO(int _KBPhauThuatID)
		{
			DataPortal.Delete(new RootCriteria(_KBPhauThuatID));
		}

		public static void DeleteKBPhauThuatBO(int _KBPhauThuatID, bool directDelete)
		{
			DataPortal.Delete(new RootCriteria(_KBPhauThuatID, directDelete));
		}
		#endregion Factory Methods
		//==========================================================================
		
		#region Criteria
		[Serializable()]
		public class RootCriteria
		{
			public int KBPhauThuatID;
			public bool directDelete = false;
			public RootCriteria(int _KBPhauThuatID)
			{
				this.KBPhauThuatID = _KBPhauThuatID;
				this.directDelete = false;
			}
			
			public RootCriteria(int _KBPhauThuatID, bool directDelete)
            {
                this.KBPhauThuatID = _KBPhauThuatID;
                this.directDelete = directDelete;
            }
            
			public RootCriteria() {}
		}

		[Serializable()]
		public class ChildCriteria
		{
			public int KBPhauThuatID;
			public bool directDelete = false;
			public ChildCriteria(int _KBPhauThuatID)
			{
				this.KBPhauThuatID = _KBPhauThuatID;
				this.directDelete = false;
			}
			
			public ChildCriteria(int _KBPhauThuatID, bool directDelete)
            {
                this.KBPhauThuatID = _KBPhauThuatID;
                this.directDelete = directDelete;
            }
            
			public ChildCriteria() {}
		}
		#endregion Criteria
		//==========================================================================

		#region Create
		[RunLocal]
		private void DataPortal_Create(RootCriteria criteria)
		{
		}

		private void DataPortal_Create(ChildCriteria criteria)
		{
			MarkAsChild();
			ValidationRules.CheckRules();
		}

		#endregion Create
		//==========================================================================

		#region Fetch
		[Transactional(TransactionalTypes.TransactionScope)]
		protected void DataPortal_Fetch(RootCriteria criteria)
		{
			this.Execute_Fetch(criteria);
		}

		protected void DataPortal_Fetch(string where)
		{
			this.Execute_Fetch(where);
		}

		protected void Execute_Fetch(RootCriteria criteria)
		{
			this.This_Fetch(criteria);
		}
		
		protected void Execute_Fetch(string where)
		{
			this.This_Fetch(where);
		}

		private void This_Fetch(RootCriteria criteria)
		{
			SqlDatabase db = new SqlDatabase(Session.DBConnectionString);
			SqlCommand cmd = (SqlCommand)db.GetStoredProcCommand("KBPhauThuatBO_GetBO");
			db.AddInParameter(cmd, "@WhereExpression", SqlDbType.NVarChar, "[KBPhauThuatID] = " + criteria.KBPhauThuatID);
			using (SqlDataReader dataReader = (SqlDataReader)db.ExecuteReader(cmd))
			{
				if (dataReader.Read())
					Fetch(dataReader);                    
			}
		}

		private void This_Fetch(string where)
		{
			SqlDatabase db = new SqlDatabase(Session.DBConnectionString);
			SqlCommand cmd = (SqlCommand)db.GetStoredProcCommand("KBPhauThuatBO_GetBO");
			db.AddInParameter(cmd, "@WhereExpression", SqlDbType.NVarChar, where);
			using (SqlDataReader dataReader = (SqlDataReader)db.ExecuteReader(cmd))
			{
				if (dataReader.Read())
					Fetch(dataReader);                    
			}
		}

		private void Fetch(SqlDataReader dataReader)
		{
			if (!(dataReader["KBPhauThuatID"] is DBNull))
this._KBPhauThuatID = (int)dataReader["KBPhauThuatID"];
if (!(dataReader["MaSo"] is DBNull))
this._MaSo = (string)dataReader["MaSo"];
if (!(dataReader["KhoaID"] is DBNull))
this._KhoaID = (int)dataReader["KhoaID"];
if (!(dataReader["BenhNhanID"] is DBNull))
this._BenhNhanID = (int)dataReader["BenhNhanID"];
if (!(dataReader["HoTen"] is DBNull))
this._HoTen = (string)dataReader["HoTen"];
if (!(dataReader["DiaChi"] is DBNull))
this._DiaChi = (string)dataReader["DiaChi"];
if (!(dataReader["GioiTinh"] is DBNull))
this._GioiTinh = (string)dataReader["GioiTinh"];
if (!(dataReader["DoiTuongID"] is DBNull))
this._DoiTuongID = (int)dataReader["DoiTuongID"];
if (!(dataReader["NguonXuatID"] is DBNull))
this._NguonXuatID = (int)dataReader["NguonXuatID"];
if (!(dataReader["NgayLapPhieu"] is DBNull))
this._NgayLapPhieu = (DateTime)dataReader["NgayLapPhieu"];
if (!(dataReader["NgayThucHien"] is DBNull))
this._NgayThucHien = (DateTime)dataReader["NgayThucHien"];
if (!(dataReader["NoiDung"] is DBNull))
this._NoiDung = (string)dataReader["NoiDung"];
if (!(dataReader["GhiChu"] is DBNull))
this._GhiChu = (string)dataReader["GhiChu"];
if (!(dataReader["NguoiLapPhieuID"] is DBNull))
this._NguoiLapPhieuID = (int)dataReader["NguoiLapPhieuID"];
if (!(dataReader["NguoiPhuTrachID"] is DBNull))
this._NguoiPhuTrachID = (int)dataReader["NguoiPhuTrachID"];
if (!(dataReader["ThanhTien"] is DBNull))
this._ThanhTien = (double)dataReader["ThanhTien"];
if (!(dataReader["BangChu"] is DBNull))
this._BangChu = (string)dataReader["BangChu"];
if (!(dataReader["TienPhatSinh"] is DBNull))
this._TienPhatSinh = (double)dataReader["TienPhatSinh"];
if (!(dataReader["LyDoPhatSinh"] is DBNull))
this._LyDoPhatSinh = (string)dataReader["LyDoPhatSinh"];
if (!(dataReader["ThanhToan"] is DBNull))
this._ThanhToan = (int)dataReader["ThanhToan"];
if (!(dataReader["ThucHien"] is DBNull))
this._ThucHien = (int)dataReader["ThucHien"];
if (!(dataReader["UserCreated"] is DBNull))
this._UserCreated = (int)dataReader["UserCreated"];
if (!(dataReader["DateCreated"] is DBNull))
this._DateCreated = (DateTime)dataReader["DateCreated"];
if (!(dataReader["UserModify"] is DBNull))
this._UserModify = (int)dataReader["UserModify"];
if (!(dataReader["DateModify"] is DBNull))
this._DateModify = (DateTime)dataReader["DateModify"];
if (!(dataReader["Deleted"] is DBNull))
this._Deleted = (bool)dataReader["Deleted"];

		}
		#endregion Fetch
		//==========================================================================

		#region Insert
		[Transactional(TransactionalTypes.TransactionScope)]
		protected override void DataPortal_Insert()
		{
			this.Execute_Insert();
		}

		protected void Execute_Insert()
		{
			this.This_Insert();
		}

		private void This_Insert()
		{
			this._UserCreated = Session.LoginID;
this._DateCreated = DateTime.MaxValue;
this._UserModify = null;
this._DateModify = null;
this._Deleted = false;

			SqlDatabase db = new SqlDatabase(Session.DBConnectionString);
			SqlCommand cmd = (SqlCommand)db.GetStoredProcCommand("KBPhauThuatBO_InsertBO");
			db.AddInParameter(cmd, "@MaSo", SqlDbType.NVarChar, this._MaSo);
db.AddInParameter(cmd, "@KhoaID", SqlDbType.Int, this._KhoaID);
db.AddInParameter(cmd, "@BenhNhanID", SqlDbType.Int, this._BenhNhanID);
db.AddInParameter(cmd, "@HoTen", SqlDbType.NVarChar, this._HoTen);
db.AddInParameter(cmd, "@DiaChi", SqlDbType.NVarChar, this._DiaChi);
db.AddInParameter(cmd, "@GioiTinh", SqlDbType.NVarChar, this._GioiTinh);
db.AddInParameter(cmd, "@DoiTuongID", SqlDbType.Int, this._DoiTuongID);
db.AddInParameter(cmd, "@NguonXuatID", SqlDbType.Int, this._NguonXuatID);
db.AddInParameter(cmd, "@NgayLapPhieu", SqlDbType.DateTime, this._NgayLapPhieu);
db.AddInParameter(cmd, "@NgayThucHien", SqlDbType.DateTime, this._NgayThucHien);
db.AddInParameter(cmd, "@NoiDung", SqlDbType.NVarChar, this._NoiDung);
db.AddInParameter(cmd, "@GhiChu", SqlDbType.NVarChar, this._GhiChu);
db.AddInParameter(cmd, "@NguoiLapPhieuID", SqlDbType.Int, this._NguoiLapPhieuID);
db.AddInParameter(cmd, "@NguoiPhuTrachID", SqlDbType.Int, this._NguoiPhuTrachID);
db.AddInParameter(cmd, "@ThanhTien", SqlDbType.Float, this._ThanhTien);
db.AddInParameter(cmd, "@BangChu", SqlDbType.NVarChar, this._BangChu);
db.AddInParameter(cmd, "@TienPhatSinh", SqlDbType.Float, this._TienPhatSinh);
db.AddInParameter(cmd, "@LyDoPhatSinh", SqlDbType.NVarChar, this._LyDoPhatSinh);
db.AddInParameter(cmd, "@ThanhToan", SqlDbType.Int, this._ThanhToan);
db.AddInParameter(cmd, "@ThucHien", SqlDbType.Int, this._ThucHien);
db.AddInParameter(cmd, "@UserCreated", SqlDbType.Int, this._UserCreated);
db.AddOutParameter(cmd, "@NewKBPhauThuatID", SqlDbType.Int, 10);
db.AddOutParameter(cmd, "@NewDateCreated", SqlDbType.DateTime, 4);
			db.ExecuteNonQuery(cmd);
			this._KBPhauThuatID = (int)cmd.Parameters["@NewKBPhauThuatID"].Value;
this._DateCreated = (DateTime)cmd.Parameters["@NewDateCreated"].Value;
		}
		#endregion Insert
		//==========================================================================

		#region Update
		[Transactional(TransactionalTypes.TransactionScope)]
		protected override void DataPortal_Update()
		{
			this.Execute_Update();
		}

		protected void Execute_Update()
		{
			this.This_Update();
		}

		private void This_Update()
		{
			this._UserModify = Session.LoginID;
this._DateModify = DateTime.Now;

			SqlDatabase db = new SqlDatabase(Session.DBConnectionString);
			SqlCommand cmd = (SqlCommand)db.GetStoredProcCommand("KBPhauThuatBO_UpdateBO");
			db.AddInParameter(cmd, "@KBPhauThuatID", SqlDbType.Int, this._KBPhauThuatID);
db.AddInParameter(cmd, "@MaSo", SqlDbType.NVarChar, this._MaSo);
db.AddInParameter(cmd, "@KhoaID", SqlDbType.Int, this._KhoaID);
db.AddInParameter(cmd, "@BenhNhanID", SqlDbType.Int, this._BenhNhanID);
db.AddInParameter(cmd, "@HoTen", SqlDbType.NVarChar, this._HoTen);
db.AddInParameter(cmd, "@DiaChi", SqlDbType.NVarChar, this._DiaChi);
db.AddInParameter(cmd, "@GioiTinh", SqlDbType.NVarChar, this._GioiTinh);
db.AddInParameter(cmd, "@DoiTuongID", SqlDbType.Int, this._DoiTuongID);
db.AddInParameter(cmd, "@NguonXuatID", SqlDbType.Int, this._NguonXuatID);
db.AddInParameter(cmd, "@NgayLapPhieu", SqlDbType.DateTime, this._NgayLapPhieu);
db.AddInParameter(cmd, "@NgayThucHien", SqlDbType.DateTime, this._NgayThucHien);
db.AddInParameter(cmd, "@NoiDung", SqlDbType.NVarChar, this._NoiDung);
db.AddInParameter(cmd, "@GhiChu", SqlDbType.NVarChar, this._GhiChu);
db.AddInParameter(cmd, "@NguoiLapPhieuID", SqlDbType.Int, this._NguoiLapPhieuID);
db.AddInParameter(cmd, "@NguoiPhuTrachID", SqlDbType.Int, this._NguoiPhuTrachID);
db.AddInParameter(cmd, "@ThanhTien", SqlDbType.Float, this._ThanhTien);
db.AddInParameter(cmd, "@BangChu", SqlDbType.NVarChar, this._BangChu);
db.AddInParameter(cmd, "@TienPhatSinh", SqlDbType.Float, this._TienPhatSinh);
db.AddInParameter(cmd, "@LyDoPhatSinh", SqlDbType.NVarChar, this._LyDoPhatSinh);
db.AddInParameter(cmd, "@ThanhToan", SqlDbType.Int, this._ThanhToan);
db.AddInParameter(cmd, "@ThucHien", SqlDbType.Int, this._ThucHien);
db.AddInParameter(cmd, "@UserModify", SqlDbType.Int, this._UserModify);
db.AddInParameter(cmd, "@Deleted", SqlDbType.Bit, this._Deleted);

db.AddOutParameter(cmd, "@NewDateModify", SqlDbType.DateTime, 4);
			db.ExecuteNonQuery(cmd);
			
this._DateModify = (DateTime)cmd.Parameters["@NewDateModify"].Value;
		}
		#endregion Update
		//==========================================================================

		#region Delete
		[Transactional(TransactionalTypes.TransactionScope)]
		protected override void DataPortal_DeleteSelf()
		{
			DataPortal_Delete(new RootCriteria(_KBPhauThuatID.Value));
		}

		private void DataPortal_Delete(RootCriteria criteria)
		{
			this.Execute_Delete(criteria);
		}

		protected void Execute_Delete(RootCriteria criteria)
		{
			SqlDatabase db = new SqlDatabase(Session.DBConnectionString);
			SqlCommand cmd = (SqlCommand)db.GetStoredProcCommand("KBPhauThuatBO_DeleteBO");
			db.AddInParameter(cmd, "@DirectDelete", SqlDbType.Bit, criteria.directDelete);
			db.AddInParameter(cmd, "@KBPhauThuatID", SqlDbType.Int, criteria.KBPhauThuatID);
			db.ExecuteNonQuery(cmd);
		}
		#endregion Delete
		//==========================================================================

		#region Internal method
		internal void Insert()
		{
			if (!this.IsDirty) return;
			DataPortal_Insert();
			MarkOld();
		}

		internal void Update()
		{
			if (!this.IsDirty) return;
			DataPortal_Update();
			MarkOld();
		}

		internal void DeleteSelf()
		{
			if (!this.IsDirty) return;
			if (this.IsNew) return;
			DataPortal_Delete(new RootCriteria(_KBPhauThuatID.Value));
			MarkNew();
		}
		#endregion Internal method
		//==========================================================================
	}
}
